﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports roncliProductions.LibWowArmory.Dungeon.Strings
Imports System
Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Linq
Imports System.Web
Imports System.Xml.Serialization

Namespace roncliProductions.LibWowArmory.Dungeon

    ''' <summary>
    ''' A class that caches the dungeons and bosses available on the Armory
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Dungeons
        Inherits WowArmoryData

        Private dsConfig As New Schema.config
        Private dsData As DungeonStrings

        ''' <summary>
        ''' The URL of the dungeon and boss data
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL As String
            Get
                Return "http://www.wowarmory.com/_data/dungeons.xml"
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request does not require login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Returns a list of available dungeons
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Dungeons As Collection(Of Dungeon)
            Get
                If dsConfig Is Nothing Then
                    Return New Collection(Of Dungeon)
                End If
                Return (From d In dsConfig.dungeons(0).dungeon
                        Select New Dungeon(d.hasHeroic = 1, d.id, d.key, d.levelMax, d.levelMin, GetDungeonName(d.id, d.key), d.partySize, d.raid = 1,
                               CType(d.release, Enums.ReleaseType),
                               (From b In d.bosses.SelectMany(Function(b) b.boss) Select New Boss(b.id, GetBossName(d.id, d.key, b.id))).ToCollection)).ToCollection
            End Get
        End Property

        ''' <summary>
        ''' Constructor that retrieves the dungeon and boss data from either the armory or the local cache
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            If Cache.Cache("LibWowArmory.Dungeons") Is Nothing Then
                MyBase.Retrieve()

                Dim xsSerializer As New XmlSerializer(dsConfig.GetType)

                Using srXML As New StringReader(MyBase.Data)
                    dsConfig = CType(xsSerializer.Deserialize(srXML), Schema.config)
                End Using

                Cache.Cache.Insert("LibWowArmory.Dungeons", dsConfig, Nothing, Date.UtcNow.AddDays(1), Nothing, Caching.CacheItemPriority.NotRemovable, Nothing)
            Else
                dsConfig = CType(Cache.Cache("LibWowArmory.Dungeons"), Schema.config)
            End If

            dsData = New DungeonStrings
        End Sub

        Private Function GetDungeonName(ByVal intDungeonID As Integer, ByVal strKey As String) As String
            Dim varDungeon = dsData.Dungeons.Where(Function(d) d.DungeonID = intDungeonID And d.Key = strKey)
            If varDungeon.Count = 0 Then
                Return Nothing
            Else
                Return varDungeon.First.Name
            End If
        End Function

        Private Function GetBossName(ByVal intDungeonID As Integer, ByVal strKey As String, ByVal intBossID As Integer) As String
            Dim varDungeon = dsData.Dungeons.Where(Function(d) d.DungeonID = intDungeonID And d.Key = strKey)
            If varDungeon.Count = 0 Then
                Return Nothing
            Else
                Dim dDungeon As Strings.Dungeon = varDungeon.First
                Dim varBoss = dDungeon.Bosses.Where(Function(b) b.BossID = intBossID)
                If varBoss.Count = 0 Then
                    Return Nothing
                Else
                    Return varBoss.First.Name
                End If
            End If
        End Function

    End Class

End Namespace